﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace qiniuUI
{
    public static class ConfigHelper
    {
        //依据连接串名字connectionName返回数据连接字符串  
        public static string GetConnectionStringsConfig(string connectionName)
        {
            XmlDocument xmldoc = new XmlDocument();
            bool check_fail = false;
            XmlNode name = null;
            XmlNode root = null;
            string connectiontext = null;
            try
            {
                xmldoc.Load(System.Environment.CurrentDirectory + "\\qiniuUI.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show(System.Environment.CurrentDirectory + "\\qiniuUI.xml" + "不存在.\r\n" + ex.ToString());
                check_fail = true;
            }
            if (!check_fail)
            {
                try
                {
                    root = xmldoc.SelectSingleNode("configuration");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("配置文件一定要有configuration节点\r\n" + ex.ToString());
                }
                try
                {
                    name = root.SelectSingleNode(connectionName);
                    connectiontext = name.InnerText;
                }
                catch
                {
                    //MessageBox.Show("配置文件中" + connectionName + "节点不存在configureation父节点中.\r\n" + ex.ToString());
                    connectiontext = "";
                }
            }
            else
            {
                connectiontext = "";
            }
            return connectiontext;
        }
        public static void change_config_txt(List<string> connectionName, List<string> text)
        {
            XmlDocument xmldoc = new XmlDocument();
            bool check_fail = false;
            XmlNode name = null;
            XmlNode root = null;
            int i;
            try
            {
                xmldoc.Load(System.Environment.CurrentDirectory + "\\qiniuUI.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show(System.Environment.CurrentDirectory + "\\qiniuUI.xml" + "不存在.\r\n" + ex.ToString());
                check_fail = true;
            }
            if (!check_fail)
            {
                try
                {
                    root = xmldoc.SelectSingleNode("configuration");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("配置文件一定要有configuration节点\r\n" + ex.ToString());
                }
                if (text.Count == connectionName.Count)
                {
                    for (i = 0; i < text.Count; i++)
                    {
                        try
                        {
                            name = root.SelectSingleNode(connectionName[i]);
                            name.InnerText = text[i];
                        }
                        catch
                        {
                            XmlElement newNode = xmldoc.CreateElement(connectionName[i]);
                            newNode.InnerText = text[i];
                            root.AppendChild(newNode);
                        }
                    }
                    xmldoc.Save(System.Environment.CurrentDirectory + "\\qiniuUI.xml");
                }
            }
        }
    }
}
